package com.wno704.quartz.service.impl;


import com.wno704.quartz.config.WnoConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.wno704.quartz.entity.JobLog;
import com.wno704.quartz.service.JobLogService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("JobLogService")
public class JobLogServiceImpl implements JobLogService {

    @Autowired
    @Qualifier("jdbcTemplateQuartz")
    private JdbcTemplate jdbcTemplateQuartz;

    @Autowired
    WnoConfig wnoConfig;

    @Override
    public void saveJobLog(JobLog jobLog) {
        if (wnoConfig.getQuartzDB().equals("MySQL")) {
            String sql = "INSERT INTO QRTZ_JOB_LOG (JOB_ID,BEAN_NAME,METHOD_NAME,PARAMS,STATUS,ERROR,TIMES,CREATE_TIME) VALUES (?,?,?,?,?,?,?,NOW())";
            this.jdbcTemplateQuartz.update(sql,
                    new Object[]{jobLog.getJobId(), jobLog.getBeanName(),
                            jobLog.getMethodName(), jobLog.getParams(), jobLog.getStatus(),
                            jobLog.getErrorInfo(), jobLog.getTimes()
                    });
        } else if (wnoConfig.getQuartzDB().equals("Oracle")) {
            String sql = "INSERT INTO QRTZ_JOB_LOG (LOG_ID,JOB_ID,BEAN_NAME,METHOD_NAME,PARAMS,STATUS,ERROR,TIMES,CREATE_TIME) VALUES (SEQ_QRTZ_JOB_LOG_ID.Nextval,?,?,?,?,?,?,?,sysdate)";
            this.jdbcTemplateQuartz.update(sql,
                    new Object[]{jobLog.getJobId(), jobLog.getBeanName(),
                            jobLog.getMethodName(), jobLog.getParams(), jobLog.getStatus(),
                            jobLog.getErrorInfo(), jobLog.getTimes()
                    });
        } else if (wnoConfig.getQuartzDB().equals("PostgreSQL")) {
            String sql = "INSERT INTO QRTZ_JOB_LOG (LOG_ID,JOB_ID,BEAN_NAME,METHOD_NAME,PARAMS,STATUS,ERROR,TIMES,CREATE_TIME) VALUES (nextval('SEQ_QRTZ_JOB_LOG_ID'),?,?,?,?,?,?,?,NOW())";
            this.jdbcTemplateQuartz.update(sql,
                    new Object[]{jobLog.getJobId(), jobLog.getBeanName(),
                            jobLog.getMethodName(), jobLog.getParams(), jobLog.getStatus(),
                            jobLog.getErrorInfo(), jobLog.getTimes()
                    });
        }
    }
}
